<?php

namespace zhtfrontend\models\datas;

use common\models\datas\Student;
use Yii;

/**
 * This is the model class for table "{{%student_check}}".
 *
 * @property integer $id
 * @property integer $class_student_id
 * @property integer $student_id
 * @property integer $status
 * @property integer $created_at
 * @property integer $class_lesson_id
 */
class StudentCheck extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%student_check}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['class_student_id', 'order_sales_id', 'student_id', 'status', 'created_at','class_lesson_id','is_makeup'], 'integer'],
            [['student_id','remarks'], 'required'],
            [['lesson_type','remarks'], 'string'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', 'ID'),
            'class_student_id' => Yii::t('zhtfrontend', '学生表编号'),
            'order_sales_id' => Yii::t('zhtfrontend', '订单记录id'),
            'student_id' => Yii::t('zhtfrontend', '学生编号'),
            'status' => Yii::t('zhtfrontend', '学员状态 0未打卡 1打卡 2关闭打卡'),
            'created_at' => Yii::t('zhtfrontend', '签到时间'),
            'class_lesson_id' => Yii::t('zhtfrontend', '课次'),
            'lesson_type' => Yii::t('zhtfrontend', '课次类型'),
            'is_makeup' => Yii::t('zhtfrontend', '请假是否补课'),
            'remarks' => Yii::t('zhtfrontend', '备注'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        return $fields;
    }

    public function extraFields()
    {
        $fields = parent::extraFields(); // TODO: Change the autogenerated stub
        $fields['customer_name'] = function (){
            return isset($this->student->customer->name)?$this->student->customer->name:null;
        };
        $fields['course_name'] = function (){
            $classlesson = $this->classlesson;
            if(!empty($classlesson)){
                return $classlesson->title;
            }else{
                return null;
            }
        };
        $fields['course_time'] = function (){
            $classlesson = $this->classlesson;
            if(!empty($classlesson)){
                $date = $classlesson->class_date;
                $date = \Yii::$app->formatter->asDatetime($date);
                $start_time = $classlesson->start_time;
                $end_time = $classlesson->end_time;
                $len = strlen($start_time) - 2;
                $lenth = strlen($end_time) - 2;
                $start_time = ($len == 1) ? ('0' . floor($start_time / 100) . ':' . substr($start_time, 1)) : (floor($start_time / 100) . ':' . substr($start_time, 2));
                $end_time = ($lenth == 1) ? ('0' . floor($end_time / 100) . ':' . substr($end_time, 1)) : (floor($end_time / 100) . ':' . substr($end_time, 2));
                $time = (empty($date)) ? ($start_time . '-' . $end_time) : ($date . ' ' . $start_time . '-' . $end_time);
                return $time;
            }else{
                return null;
            }
        };
        $fields['course_num'] = function (){
            return isset($this->classlesson)?$this->classlesson->lesson:null;
        };
        $fields['teacher_name'] = function(){
            $goods = isset($this->classlesson)?$this->classlesson->classManage->goods:null;
            if(!empty($goods)){
                $arr = Arrangement::findOne($goods->arrangement_id);
                if(!empty($arr)){
                    return isset($arr->teacher)?$arr->teacher->name:null;
                }
            }
            return null;
        };
        $fields['teache_room'] = function (){
            $classlesson = isset($this->classlesson)?$this->classlesson:null;
            if(!empty($classlesson)){
                $classmanage = isset($classlesson->classManage)?$classlesson->classManage:null;
                if(!empty($classmanage)){
                    $classmanageattribute = isset($classmanage->classmanageattribute)?$classmanage->classmanageattribute:null;
                    if(!empty($classmanageattribute)){
                        if(isset($classmanageattribute->room_type) && $classmanageattribute->room_type == 1){
                            $room_name = '';
                            if(!empty($classmanageattribute->classroom_name)){
                                $room_name = '-'.$classmanageattribute->classroom_name;
                            }
                            return isset($classmanageattribute->room_group_name)?$classmanageattribute->room_group_name.$room_name:'';
                        }else{
                            return isset($classmanageattribute->classroom_name) ? $classmanageattribute->classroom_name : '';
                        }
                    }

                }
            }
            return '';
        };
        return $fields;
    }

    public function getOrderitems()
    {
        return $this->hasOne(OrdersItems::className(), ['id' => 'order_sales_id']);
    }

    public function getStudent()
    {
        return $this->hasOne(Student::className(), ['id' => 'student_id']);
    }

    public function getClasslesson()
    {
        return $this->hasOne(ClassLesson::className(), ['lesson_id' => 'class_lesson_id']);
    }



}
